﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Truextend.CoffeShop.Presenter.Orders;
using System.Data;
using System.Web.UI.WebControls;

namespace Truextend.CoffeShop.Layouts.Truextend.CoffeShop
{
    public partial class ApproveOrRejectOrder : LayoutsPageBase, IApproveOrRejectOrder
    {
        ApproveOrRejectOrderPresenter presenter = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter = new ApproveOrRejectOrderPresenter(this);
            presenter.FillView();

            if(!IsPostBack){                
                presenter.FillView();
                HFOrderId.Value = this.orderId.ToString();
            }

        }

        protected void OptionsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int detailId = int.Parse(OptionsGridView.DataKeys[e.RowIndex].Value.ToString());                        
                        
            presenter.DeleteDetailOfAnOrder(detailId);            
            presenter.FillOptions();

        }

        protected void BtnApprove_Click(object sender, EventArgs e)
        {
            presenter.ApproveOrder();
            Response.Redirect("~/CoffeeShop/SitePages/Home.aspx");
        }

        protected void BtnReject_Click(object sender, EventArgs e)
        {
            presenter.RejectedOrder();
            Response.Redirect("~/CoffeeShop/SitePages/Home.aspx");
        }           
        
        #region Implements IApprovedOrRejectOrder
        public string address
        {
            get
            {
                return LblAddressContent.Text;
            }
            set
            {
                LblAddressContent.Text = value;
            }
        }

        public System.Data.DataTable options
        {
            get
            {
                DataTable dataTable = new DataTable();

                dataTable.Columns.Add("OptionID");
                dataTable.Columns.Add("Description");
                dataTable.Columns.Add("Amount");
                dataTable.Columns.Add("Price");

                for (int i = 0; i < OptionsGridView.Rows.Count; i++ )
                {
                    DataRow row = dataTable.NewRow();                    
                    GridViewRow optionRow = this.OptionsGridView.Rows[i];
                    row["OptionID"] = OptionsGridView.DataKeys[i].Value.ToString();
                    row["Description"] = optionRow.FindControl("LblDescription");
                    row["Amount"] = optionRow.FindControl("LblAmount");
                    row["Price"] = optionRow.FindControl("LblPrice");

                    dataTable.Rows.Add(row);
                }

                return dataTable;
            }
            set
            {
                OptionsGridView.DataSource = value;
                OptionsGridView.Columns[0].Visible = false;
                OptionsGridView.DataBind();
            }
        }

        public int orderId
        {
            get
            {
                return int.Parse(Request.QueryString["orderId"].ToString());
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string orderTitle
        {
            get
            {
                return LblTitleContent.Text;
            }
            set
            {
                LblTitleContent.Text = value;
            }
        }

        public string phones
        {
            get
            {
                return LblPhonesContent.Text;
            }
            set
            {
                LblPhonesContent.Text = value;
            }
        }

        public string restaurantName
        {
            get
            {
                return LblRestaurantContent.Text;
            }
            set
            {
                LblRestaurantContent.Text = value;
            }
        }

        public float totalPrice
        {
            get
            {
                return float.Parse(LblTotalPriceContent.Text);
            }
            set
            {
                LblTotalPriceContent.Text = value.ToString();
            }
        }
        #endregion

    }
}
